package com.hw.one.collect.radar.util;

import java.text.DecimalFormat;

public class Rasterize {

    private static DecimalFormat df6 = new DecimalFormat("#0.000000");

    /**
     * 判断点位在哪个栅格中
     *
     * @param x
     * @param y
     * @param step
     * @return
     */

    public static String getGrid(double x, double y, double radius, double step) {
        if (Math.abs(x) == radius || Math.abs(y) == radius) {
            if (y == radius) {
                return "0.0##" + y;
            } else if (x == -radius) {
                return x + "##0.0";
            } else if (x == radius) {
                return x - step + "##0.0";
            } else if (y == -radius) {
                return "0.0##" + (y + step);
            } else {
            }
        }
        double x2 = 0;
        double y2 = 0;
        if (x % step == 0d && y % step == 0d) {
            x2 = x;
            y2 = y;
        }
        if (y > 0) {
            if (x < 0) {
                x2 = (Math.abs(x) + (step - Math.abs(x) % step)) * -1;
            } else {
                x2 = x - x % step;
            }
            y2 = y + (step - y % step);
        } else {
            if (x < 0) {
                x2 = (Math.abs(x) + (step - Math.abs(x) % step)) * -1;
            } else {
                x2 = x - x % step;
            }
            y2 = y - y % step;
        }

        return x2 + "##" + y2;
    }


    /**
     * 将雷达扫描的角度转换成X坐标下的角度
     *
     * @param orgAngle
     * @return
     */

    public static int getReXAngle(int orgAngle) {
        return (450 - orgAngle) % 360;
    }

    /**
     * 获取坐标系中的X值
     *
     * @param length
     * @param angle
     * @return
     */

    public static double getXPoint(double length, int angle) {
        return Double.valueOf(df6.format(length * Math.cos(angle * Math.PI / 180)));
    }

    /**
     * 获取坐标系中的Y值
     *
     * @param length
     * @param angle
     * @return
     */

    public static double getYPoint(double length, int angle) {
        return Double.valueOf(df6.format(length * Math.sin(angle * Math.PI / 180)));
    }

    /**
     * 将雷达扫描的点位转换成Y坐标下的角度
     *
     * @param x
     * @param y
     * @return
     */

    public static double getReYAngle(double x, double y) {
        // 反着Math.atan2用的
        return (360 + Math.atan2(x, y) * 180 / Math.PI) % 360;
    }

    /**
     * 将雷达扫描的点位转换成X坐标下的角度（暂不用）
     *
     * @param x
     * @param y
     * @return
     */

    public static double getReXAngle(double x, double y) {
        return (450 - Math.atan2(y, x) * 180 / Math.PI) % 360;
    }

    /**
     * 计算斜边的长
     *
     * @param a
     * @param b
     * @return
     */

    public static double getPonitDistance(double a, double b) {
        return Math.sqrt(a * a + b * b);
    }

    /**
     * 根据坐标系中的x,y，计算栅格四个顶点的经纬度
     *
     * @param x
     * @param y
     * @return
     */
    public static double[][] getPointLoc(double x, double y, double step, LocationUtil locationUtil) {

        double[][] loc = new double[4][2];

        loc[0] = LocationUtil.getMyLatLng(locationUtil, getPonitDistance(x, y), getReYAngle(x, y));
        loc[1] = LocationUtil.getMyLatLng(locationUtil, getPonitDistance(x + step, y), getReYAngle(x + step, y));
        loc[2] = LocationUtil.getMyLatLng(locationUtil, getPonitDistance(x + step, y - step), getReYAngle(x + step, y - step));
        loc[3] = LocationUtil.getMyLatLng(locationUtil, getPonitDistance(x, y - step), getReYAngle(x, y - step));

        return loc;
    }

    /**
     * 栅格中心点的经纬度
     *
     * @param x
     * @param y
     * @param locationUtil
     * @return
     */

    public static double[] getGridPonitLoc(double x, double y, LocationUtil locationUtil) {
        return LocationUtil.getMyLatLng(locationUtil, getPonitDistance(x, y), getReYAngle(x, y));
    }

}
